package com.zyz.M_File和IO流.IO流.缓冲流.BufferedInputStream和BufferedOutputStream;

import java.io.*;

/**
 * @author: vick
 * @Date: 2025/4/8
 * @Description:
 * @version: 1.0
 */
//非文本文件复制
public class Demo1 {
    public static void main(String[] args) {

        String srcPath = "D:\\software\\录屏\\86-IO流-字符流-文本文件-FileWriter、综合练习.mp4";
        String destPath = "D:\\software\\录屏\\86-IO流-字符流-文本文件-FileWriter、综合练习2.mp4";

        /*long start = System.currentTimeMillis();
        copyFileWithBufferedStream(srcPath, destPath);
        long end = System.currentTimeMillis();
        System.out.println("花费时间：" + (end - start));//1493*/

        long start = System.currentTimeMillis();
        copyFileWithFileStream(srcPath, destPath);
        long end = System.currentTimeMillis();
        System.out.println("花费时间：" + (end - start));//16115
    }

    //使用节点流
    private static void copyFileWithFileStream(String srcPath, String destPath) {
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            //1. 创建File对象
            File source = new File(srcPath);
            File copy = new File(destPath);

            //2. 创建流对象
            fileInputStream = new FileInputStream(source);
            fileOutputStream = new FileOutputStream(copy);

            //3. 复制操作 （读写）
            byte[] buffer = new byte[100];
            //每次读入到 buffer中的字节数
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭资源
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //使用缓冲流
    public static void copyFileWithBufferedStream(String srcPath, String destPath) {
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            //1. 创建File对象
            File source = new File(srcPath);
            File copy = new File(destPath);

            //2. 创建流对象对象
            FileInputStream fileInputStream = new FileInputStream(source);
            FileOutputStream fileOutputStream = new FileOutputStream(copy);

            //3. 创建缓冲流
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            byte[] bytes = new byte[100];
            int len;
            while ((len = bufferedInputStream.read(bytes)) != -1) {
                bufferedOutputStream.write(bytes, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //先关闭外层的流，再关闭内层的流
            if (bufferedOutputStream != null) {
                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (bufferedInputStream != null) {
                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
